package action;

import model.PageBean;
import model.TableInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import util.ResponseUtil;
import dao.TableInfoDao;

public class TableInfoAction extends BaseAction {

	private static final long serialVersionUID = 354617898023902887L;
	private String page;
	private String rows;
	private String keyword;
	private TableInfo tableInfo;
	private String tableName;
	
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public TableInfo getTableInfo() {
		return tableInfo;
	}

	public void setTableInfo(TableInfo tableInfo) {
		this.tableInfo = tableInfo;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	TableInfoDao tableInfoDao = new TableInfoDao();

	@Override
	public String execute() throws Exception {
		PageBean pageBean = new PageBean(page == null? 0 : Integer.parseInt(page),
				rows == null ? 0 : Integer.parseInt(rows));
		JSONObject result = new JSONObject();
		try {
			JSONArray jsonArray = tableInfoDao
					.findAllTables(request.getSession(), keyword, pageBean);
			int total = tableInfoDao.getTablesCount(request.getSession(), keyword);
			result.put("rows", jsonArray);
			result.put("total", total);
			result.put("success", "true");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}
}
